/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.zw.admin.server.dto.strategy;


import com.zw.admin.server.model.ResponseBean;
import com.zw.admin.server.model.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 异常处理器
 *
 * @author Mark sunlightcs@gmail.com
 */
@Slf4j
@RestControllerAdvice
public class RRExceptionHandler {

	@ExceptionHandler(NoHandlerFoundException.class)
	public ResponseBean handlerNoFoundException(Exception e) {
		log.error(e.getMessage(), e);
		return ResponseBean.error(ResponseEnum.NO_HANDLER_FOUND_EXCEPTION);
	}

	@ExceptionHandler(DuplicateKeyException.class)
	public ResponseBean handleDuplicateKeyException(DuplicateKeyException e){
		log.error(e.getMessage(), e);
		return ResponseBean.error(ResponseEnum.DUPLICATE_KEY_EXCEPTION);
	}

	@ExceptionHandler(AuthorizationException.class)
	public ResponseBean handleAuthorizationException(AuthorizationException e){
		log.error(e.getMessage(), e);
		return ResponseBean.error(ResponseEnum.AUTHORIZATION_EXCEPTION);
	}

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(RRException.class)
	public ResponseBean handleRRException(RRException e){
		log.error(e.getMessage(), e);
		return ResponseBean.error(e.getCode(), e.getMessage());

	}

	@ExceptionHandler(Exception.class)
	public ResponseBean handleException(Exception e){
		log.error(e.getMessage(), e);
		return ResponseBean.error(ResponseEnum.UNKNOWN_EXCEPTION);
	}
}
